﻿using WMM.Tapioca;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using System.Xml;
using System.Net;
using System.Configuration;
using WMM.Tapioca.Instance;
using System;
using WMM.Tapioca.Utilities;

namespace WMM.Test.Tapioca
{
    /// <summary>
    /// Summary description for UkGaapSamples20080620UnitTest
    /// </summary>
    [TestClass]
    public class Chapter14UnderstandingXBRLDimensions
    {
        static XmlCachingResolver resolver;

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            bool useProxy = Boolean.Parse(ConfigurationManager.AppSettings["UseProxy"]);
            resolver = new XmlCachingResolver(true, useProxy);    
        }

        public Chapter14UnderstandingXBRLDimensions()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestUseCase01SalesOneFileDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-01-SalesOneFile\Sales-instance.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase02SalesModularDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-02-SalesModular\Sales-instance.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase03SectorsDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-03-Sectors\BreakdownBySector-Instance.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase04DiscontinuedOperationsDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-04-DiscontinuedOperations\DiscontinuedOperations-Instance-2005-05-15.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase05SalesPivotTableDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-05-SalesPivotTable\Sales-PivotTable-instance.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase06TypedDimensionDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-06-TypedDimension\Sales-Typed-instance.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsFalse(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase07NotAllVALIDDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-07-NotAll\Sales-instance-VALID.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase07NotAllINVALIDDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-07-NotAll\Sales-instance-INVALID.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsFalse(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase08UsingDefaultsDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-08-UsingDefaults\Sales-instance-UsesDefaults.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsFalse(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        [TestMethod]
        public void TestUseCase08UsingNoDefaultsDimension()
        {
            InstanceDocument XbrlDoc = new InstanceDocument(GetRelativePathForDocument(@"UseCase-08-UsingDefaults\Sales-instance-NoDefault.xml"), resolver);

            XbrlManager xbrlManager = new XbrlManager(XbrlDoc);            
            xbrlManager.Validate();

            Assert.IsTrue(XbrlDoc.Dts.ValidatedInstance, "The instance document was not validated.");
        }

        private string GetRelativePathForDocument(string DocFilename)
        {
            StringBuilder RelativePath = new StringBuilder();
            RelativePath.AppendFormat(@"..\..\..\WMM.Test.Tapioca\Chapter-14-UnderstandingXBRLDimensions\UseCases\{0}", DocFilename);
            return RelativePath.ToString();
        }
    }
}
